package com.sudichina.sudichina.view.customview.xlistview;

import android.app.ActionBar;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.sudichina.sudichina.R;
import com.sudichina.sudichina.utils.Toastutils;

import java.util.List;


/**
 * 自定义的对话框
 *
 * @author yangweiqiao
 */
public class ListViewDialog extends Dialog {

    private List<String> list = null;


    private Context context;
    private ListView lv_numberaddress_style;
    private MyAdapter adapter = null;
    private onRequestFinish requestfinish = null;

    public ListViewDialog(Context context, List<String> list) {

        // 访问父类的构造 传递自定义主题
        super(context, R.style.quick_option_dialog);
        this.context = context;
        this.list = list;
        // 设置对话框的位置

        getWindow().setGravity(Gravity.BOTTOM);

        //设置对话框的宽度和屏幕的宽度相等
        WindowManager m = getWindow().getWindowManager();
        Display d = m.getDefaultDisplay();
        WindowManager.LayoutParams p = getWindow().getAttributes();
        p.width = d.getWidth();
        // 设置对话框的高度
        p.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes(p);


    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 在onCreate方法中加载 对话框 布局文件
        View view = View.inflate(context, R.layout.dialog_numberaddress, null);
        // 找到listView 控件
        lv_numberaddress_style = (ListView) view
                .findViewById(R.id.lv_numberaddress_style);

        // 添加view 到当前界面
        setContentView(view);

        adapter = new MyAdapter();
        lv_numberaddress_style.setAdapter(adapter);

        // 设置条目的点击事件

//        lv_numberaddress_style
//                .setOnItemClickListener(new OnItemClickListener() {
//
//                    /**
//                     * view 就是当前点击的条目对象
//                     */
//                    @Override
//                    public void onItemClick(AdapterView<?> parent, View view,
//                                            int position, long id) {
//                        Toastutils.showToast("选择:"+mTitles[position]);
//                        adapter.notifyDataSetChanged();
//
//                        dismiss();
//                    }
//                });
    }

    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            // 自己创建个
            View view = View.inflate(getContext(),
                    R.layout.item_text, null);

            // 找到条目上的控件


            // 文字控件
            TextView tvname = (TextView) view.findViewById(R.id.item_tv_text);

            tvname.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {


                    Toastutils.showToast(list.get(position));
                    if (requestfinish != null) {

                        requestfinish.requestFinish(list.get(position), position);
                    }

                    dismiss();
                }
            });


            tvname.setText(list.get(position));

            return view;
        }

    }


    public interface onRequestFinish {

        /**
         * 数据请求完成调用
         **/
        void requestFinish(String s, int position);


    }


    public void setOnonRequestFinish(onRequestFinish requestfinish) {
        this.requestfinish = requestfinish;
    }


}
